<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appType')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appType">
              <a-select v-model="model.appType" :placeholder="$t('common.form.rule.select',{label: $t('columns.appType')})">
                <a-select-option v-for="item in bizTypeOptions" :key="item.value" :value="item.value">{{item.label}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appCode')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appCode">
              <a-input v-model="model.appCode" :placeholder="$t('common.form.rule.input',{label: $t('columns.appCode')})"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appName')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appName">
              <a-input v-model="model.appName" :placeholder="$t('common.form.rule.input',{label: $t('columns.appName')})"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appLogo')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appLogo">
              <j-image-upload  :text="$t('common.form.upload')" v-model="model.appLogo" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appInfo')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appInfo">
              <a-textarea v-model="model.appInfo" rows="4" :placeholder="$t('common.form.rule.input',{label: $t('columns.appInfo')})" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appDesc')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appDesc">
              <a-textarea v-model="model.appDesc" rows="4" :placeholder="$t('common.form.rule.input',{label: $t('columns.appDesc')})" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.appImage')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appImage">
              <j-image-upload  :text="$t('common.form.upload')" v-model="model.appImage" :isMultiple="true"></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<i18n>
  {
    "zh-CN": {
      "columns": {
        "appType": "应用类型",
        "appCode": "应用编号",
        "appName": "应用名称",
        "appLogo": "应用LOGO",
        "appInfo": "应用说明",
        "appDesc": "应用描述",
        "appImage": "应用截图"
      }
    },
    "en-US": {
      "columns": {
        "appType": "Application Type",
        "appCode": "Application Number",
        "appName": "Application Name",
        "appLogo": "Application LOGO",
        "appInfo": "Application Introduction",
        "appDesc": "Application Description",
        "appImage": "Application Screenshot"
      }
    },
    "fr-FR": {
      "columns": {
        "appType": "Type d'application",
        "appCode": "Numéro d'application",
        "appName": "Nom de l'application",
        "appLogo": "Appliquer le logo",
        "appInfo": "Introduction à l'application",
        "appDesc": "Description de l'application",
        "appImage": "Capture d'écran de l'application"
      }
    }
  }
</i18n>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AppInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/app/info/add",
          edit: "/app/info/edit",
          queryById: "/app/info/queryById",
          dict: "/app/enum-dict"
        },
        bizTypeOptions: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.initDictConfig();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      initDictConfig(){
        getAction(this.url.dict + "/app_biz_type_dict").then(res=>{
          if(res.success){
            this.bizTypeOptions = res.result;
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>